<?php

namespace App\Http\Controllers\OpenShop\FeishuMini;

use App\Enums\BaseErrorCode;
use App\Enums\OpenShop\InvestmentPromotion\SiteLocationEnum;
use App\Exceptions\Custom\ResponseHttpException;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\AppletAuth;
use App\Http\Validate\OpenShop\InvestmentPromotion\OrderValidate;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Service\OpenShop\Feishu\PermissionService;
use App\Service\OpenShop\FeishuMini\ConsultantsService;
use App\Service\OpenShop\OrderService;
use App\Service\OpenShop\User\RmsUserService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class ConsultantsController extends BaseController
{
    /**
     * 获得顾问列表
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    public function lists(Request $request)
    {
        $this->validate($request, [
            'orderNo'          => 'required|string',
        ]);

        // 不同路由获取
        $action     = request()->route()->getAction();
        $middleware = attributeGet($action, "middleware", []);
        $userInfo   = $allow = [];
        if (!in_array('kmsAuth:kmsApi', $middleware)) {
            $userInfo = AppletAuth::$userInfo;
            $allow    = PermissionService::changeManagerList();
        }
        return $this->success(ConsultantsService::lists($userInfo, $allow, $request->get('orderNo')));
    }

    /**
     * 更换顾问
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     * @throws \Illuminate\Validation\ValidationException
     */
    public function replace(Request $request)
    {
        $params = OrderValidate::changeAppletManagerValidate($request->all());

        // 不同路由获取
        $action     = request()->route()->getAction();
        $middleware = attributeGet($action, "middleware", []);
        $allow = [];
        if (!in_array('kmsAuth:kmsApi', $middleware)) {
            $allow    = PermissionService::changeManagerList();
            $userInfo = (object)[
                'id'       => AppletAuth::$userInfo->userId,
                'username' => AppletAuth::$userInfo->username,
            ];
        } else {
            $userInfo = (object)[
                'id'       => request()->user()->rmsUserId,
                'username' => request()->user()->username,
            ];
        }
        if (!in_array($params->type, $allow)) {
            throw new ResponseHttpException(BaseErrorCode::$UN_SUPPORT_OPERATION);
        }
        $paramsFormat = (object)[
            'id'          => $params->id,
            'type'        => $params->type,
            'managerId'   => $params->managerId,
            'managerName' => RmsUserService::getRmsUserInfoByUserIds([$params->managerId])['data'][0]['username'] ?? '',
        ];
        OrderService::changeManager($userInfo, $paramsFormat);
        return $this->success();
    }

}
